/**
	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package br.biofoco.p2p.rpc.rest;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;
import org.mortbay.xml.XmlConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;

import br.biofoco.p2p.rpc.EndPoint;
import br.biofoco.p2p.rpc.Protocol;
import br.biofoco.p2p.rpc.TransportManager;
import br.biofoco.p2p.rpc.messaging.CommandHandler;
import br.biofoco.p2p.rpc.messaging.Transport;
import br.biofoco.p2p.rpc.messaging.TransportClient;

public class RestTransportServer implements Transport {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(RestTransportServer.class);
	
	private Server server;	
	private volatile boolean running = false;
	private ServletHolder servletHolder;	
	private TransportManager messaging;
	private int port;

	private XmlConfiguration config;
	
	public RestTransportServer(int port) {
		this.port = port;
	}


	public boolean isRunning() {
		return running;
	}

	public void setManager(TransportManager messagingService) {
		messaging = messagingService;		
	}


	private void loadConfigFile(String webConfig) throws FileNotFoundException, SAXException, IOException {
		if (webConfig == null) 
			throw new IllegalArgumentException("XML file cannot be null!");		
		config = new XmlConfiguration(new FileInputStream(webConfig));
	}

	@Override
	public TransportClient createClient(EndPoint endpoint) throws IOException {
		return (TransportClient) new RestTransportClient();
	}

	@Override
	public Protocol getProtocol() {
		return Protocol.REST;
	}

	@Override
	public boolean isActive() {
		return running;
	}

	@Override
	public void setCommandHandler(CommandHandler handler) {
		throw new UnsupportedOperationException("Operation not supported!");
		
	}

	@Override
	public void start() throws Exception {
		LOGGER.debug("Starting REST transport ...");
		
		loadConfigFile("web-jetty.xml");
		
		System.setProperty("jetty.port", Integer.toString(port));		
        server = new Server();
        Context context = new Context(server, "/", Context.SESSIONS);

		if (config != null)  // NEW
			config.configure(server); // NEW
        
        server.start();	
		running = true;	
		
	}

	@Override
	public void stop() throws Exception {
		server.stop();		
	}

}

